package com.news.web.servlet.news;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.news.entity.News;
import com.news.entity.PageBean;
import com.news.entity.Review;
import com.news.service.NewsService;
import com.news.service.ReviewService;
import com.news.service.impl.NewsServiceImpl;
import com.news.service.impl.ReviewServiceImpl;

/**
 * Servlet implementation class NewsCategoryServlet
 */
@WebServlet("/NewsCategoryServlet")
public class NewsCategoryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 设置编码方式
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		// 创建NewsService实例
		NewsService newsService = new NewsServiceImpl();
		// 创建ReviewService实例
		ReviewService reviewService = new ReviewServiceImpl();
		// 创建map
		Map<String, Object> categoryMap = new HashMap<String, Object>();
		
		// 获取传入参数
		String newsType = request.getParameter("newsType");
		categoryMap.put("newsType", newsType);
		// 判断是否有传值
		
		if(newsType==null||"".equals(newsType)) {
			// 将数据存入
			System.out.println("条主页了");
			request.setAttribute("categoryMap", null);
			// 跳转回原页面
			request.getRequestDispatcher("index.jsp").forward(request, response);
			return;
		}
		// 执行传入参数判断
		// 处理页数
		String result = request.getParameter("currentPageNum");
		int currentPageNum = Integer.parseInt(result == null ? "1" : result);
		String pageSizeResult = request.getParameter("pageSize");
		int pageSize = Integer.parseInt(result == null ? "5" : pageSizeResult);
		// 调用分页查询方法,传入查询类型。查询页数，查询条数
		PageBean<News> date = newsService.pageList(newsType, currentPageNum, pageSize);
		// 数据存入map
		categoryMap.put("date", date);
//		System.out.println("这是date");
//		System.out.println(categoryMap.get("date"));
		// 查询右侧栏热度最高的四条新闻
		List<News> queryRightNewsHeatNumList = newsService.queryNewsHeatNum(4);
		// 装入数据
		categoryMap.put("queryRightNewsHeatNumList", queryRightNewsHeatNumList);
		// 查询右侧栏评论最高的四条新闻
		List<News> queryNewsReviewNum = newsService.queryNewsReviewNum(4);
		// 装入数据
		categoryMap.put("queryNewsReviewNum", queryNewsReviewNum);
		request.setAttribute("categoryMap", categoryMap);
		// 跳转回原页面
		System.out.println("servlet被访问了");
		request.getRequestDispatcher("/category.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
